package cn.mall.auth.controller;

import cn.mall.auth.annotation.MyPermission;
import cn.mall.auth.service.IMenuService;
import cn.mall.auth.domain.Menu;
import cn.mall.auth.query.MenuQuery;
import cn.mall.base.utils.PageList;
import cn.mall.base.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu" )
@MyPermission(name = "菜单管理")
//@MyPermission(name = "菜单管理", desc = "菜单管理层")
public class MenuController {
    @Autowired
    public IMenuService menuService;


    /**
     * 保存和修改公用的
     * @param menu 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @MyPermission(name = "菜单新增/修改管理", desc = "菜单新增/修改")
    public AjaxResult addOrUpdate(@RequestBody Menu menu) {
        try {
            if ( menu.getId() != null)
                menuService.update(menu);
            else
                menuService.insert(menu);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！" + e.getMessage());
        }
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}" )
    @MyPermission(name = "菜单删除管理", desc = "菜单删除")
    public AjaxResult remove(@PathVariable("id" ) Long id) {
        try {
            menuService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！" + e.getMessage());
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
    */
    @PatchMapping
    @MyPermission(name = "菜单批量删除管理",desc = "菜单批量删除")
    public AjaxResult patchRemove(@RequestBody List<Long> ids) {
        try {
            menuService.patchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("批量删除失败！" + e.getMessage());
        }
    }

    /**
    * 根据Id获取用户
    * @param id
    * @return
    */
    @GetMapping("/{id}" )
    public AjaxResult loadById(@PathVariable("id" ) Long id) {
        try {
            Menu menu =menuService.loadById(id);
            return AjaxResult.me().setResultObj(menu);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！" + e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult loadAll() {

        try {
            List< Menu> list = menuService.loadAll();
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！" + e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    @MyPermission(name = "菜单分页查询",desc = "菜单分页查询")
    public AjaxResult pageList(@RequestBody MenuQuery query) {
        try {
            PageList<Menu> pageList = menuService.pageList(query);
            return AjaxResult.me().setResultObj(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取分页数据失败！" + e.getMessage());
        }
    }


    /**
     * 显示菜单树
     * @return
     */
    @GetMapping("/tree")
    @MyPermission(name = "菜单树",desc = "菜单树")
    public AjaxResult loadMenuTree(){
        try {
            List<Menu> menuTrees = menuService.loadMenuTree();
            return AjaxResult.me().setResultObj(menuTrees);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取菜单树失败" + e.getMessage());
        }
    }

    /**
     * 根据ids获取菜单
     * @return
     */
    @GetMapping("/ids")
    public AjaxResult loadSns(){
        try {
            List<Long> menuIds = menuService.loadMenuIds();
            return AjaxResult.me().setResultObj(menuIds);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败" + e.getMessage());
        }
    }

    /**
     * 获取当前登录用户的菜单
     * @return
     */
    @GetMapping("/tree/{loginUserId}")
    public AjaxResult tree(@PathVariable("loginUserId") Long loginUserId){
        try {
        List<Menu> menus = menuService.tree(loginUserId);
            return AjaxResult.me().setResultObj(menus);
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取菜单失败" + e.getMessage());
        }
    }
}
